package com.hosever.coreTest2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.hosever.model.SpecialUser;
import com.hosever.model.User;

/**
 * 流测试
 * @author Hosever
 *
 */
public class StreamTest {	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		try {
//			//输出文本
//			PrintWriter out = new PrintWriter(new FileWriter("d:/IoTest.txt"),true);
//			out.println("hello java-IO");
//			
//			//输入文本(不建议使用没有读取数字的BufferedReader)
//			Scanner scanner = new Scanner(new File("d:/IoTest.txt"),"UTF-8");
//			String words = scanner.nextLine();
//			System.out.println(words);
//			
//			
//			//输出二进制
//			DataOutputStream dos = new DataOutputStream(new FileOutputStream("d:/IoTest.dat"));
//			dos.writeUTF(words);
			
			//关系对象读写
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/user.dat"));  
			User harry = new User("Harry Hacker", "123",12);  
			SpecialUser carl = new SpecialUser("Carl Hacker", "123",12); 
			User subUser = new User("sub", "123", 13);
			carl.setSubUser(subUser);
			oos.writeObject(harry);  
			oos.writeObject(carl);  
			
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:/user.dat"));
			User u1 = (User) ois.readObject();
			SpecialUser u2 = (SpecialUser) ois.readObject();
			System.out.println(u1.getName()+"|"+u2.getSubUser().getName());
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}



